package com.hushui.mall.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hushui.mall.member.dao.MemberReceiveAddressDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hushui.mall.member.entity.MemberReceiveAddressEntity;
import com.hushui.mall.member.service.MemberReceiveAddressService;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;



/**
 * 会员收货地址
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:52:42
 */
@RestController
@RequestMapping("member/memberreceiveaddress")
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    @Autowired
    private MemberReceiveAddressDao memberReceiveAddressDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据地址id查询会员的地址
     * @param addrId
     * @return
     */
    @GetMapping(value = "/{addrId}/address")
    public R getAddress(@PathVariable("addrId") Long addrId) {

        MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(addrId);

        return R.ok().put("memberReceiveAddress",memberReceiveAddress);
    }

    /**
     * 信息  :根据会员Id号，来查找会员的收货地址
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long memberId){

//		MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);

        QueryWrapper<MemberReceiveAddressEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("member_id",memberId);

        List<MemberReceiveAddressEntity> memberReceiveAddressEntities = memberReceiveAddressDao.selectList(queryWrapper);

        return R.ok().put("memberReceiveAddress", memberReceiveAddressEntities);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress){
		memberReceiveAddressService.save(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress){
		memberReceiveAddressService.updateById(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
